package com.aube.plugin.episode.list;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aube.plugin.origin.R;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.utils.SysUtil;

/**
 * Created by huyaonan on 16/5/25.
 */
public class ParallaxListTopBarLayout extends DLLayout {

    private ImageView mBack;
    private TextView mTitle;

    private int space = 40;
    private int mTitleOffsetX = 0;
    private int mTitleOffsetY = 0;
    private int MAX_OFFSET_X = 0;
    private int mTopbarHeight = 0;
    private int statusBarHeight = 0;
    private int offsetX = 0;

    public ParallaxListTopBarLayout(Context context) {
        this(context, null);
    }
    public ParallaxListTopBarLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public ParallaxListTopBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initViews();
    }

    @Override
    public void initViews() {
        space = getResources().getDimensionPixelOffset(R.dimen.widget_margin);
        mTopbarHeight = getResources().getDimensionPixelOffset(R.dimen.actionbar_height);
        mTitleOffsetX = getResources().getDimensionPixelOffset(R.dimen.widget_margin);

        statusBarHeight = SysUtil.getStatusHeight(context);

        setupData();
    }

    @Override
    public void fillData(Object o) {
    }

    private void setupData() {
        mBack = new ImageView(context);
        mBack.setScaleType(ImageView.ScaleType.CENTER);
        setImgWithRes(mBack, R.drawable.icon_new_back);
        addView(mBack);

        mTitle = new TextView(context);
        mTitle.setTextSize(20);
        mTitle.setTextColor(Color.WHITE);
        mTitle.setSingleLine(true);
        mTitle.setEllipsize(TextUtils.TruncateAt.END);
        addView(mTitle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if(mBack == null)
            return;

        mBack.measure(0, 0);
        int imgWidth = mBack.getMeasuredWidth();
        int width = getMeasuredWidth();
        MAX_OFFSET_X = imgWidth + 2*space;
//        int titleWidth = width - MAX_OFFSET_X - space;
        int titleWidth = width - offsetX - space;
        mTitle.measure(MeasureSpec.makeMeasureSpec(titleWidth, MeasureSpec.EXACTLY), 0);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if(mBack == null)
            return;

        int backY = statusBarHeight + (mTopbarHeight-mBack.getMeasuredHeight())/2;
        mBack.layout(l+space, t+backY, l+space+mBack.getMeasuredWidth(), t+backY+mBack.getMeasuredHeight());

        mTitleOffsetY = getHeight()-mTopbarHeight+(mTopbarHeight-mTitle.getHeight())/2;
        mTitle.layout(l+mTitleOffsetX, t+mTitleOffsetY, l+mTitleOffsetX+mTitle.getMeasuredWidth(), t+mTitleOffsetY+mTitle.getMeasuredHeight());
    }

    public void updateTitleOffset(int offset) {
        updateTitleTranslation(offset);
    }

    /**
     * 动态改变title的宽度
     * @param offset
     */
    public void updateTitleTranslation(int offset) {
        offsetX = (int) (offset*1f*(MAX_OFFSET_X-mTitleOffsetX)/(getHeight()-mTopbarHeight -statusBarHeight));
        mTitle.setTranslationX(offsetX);
        mTitle.setTranslationY(-offset);
        requestLayout();
    }

    public void setTopbarHeight(int height) {
        this.mTopbarHeight = height;
        requestLayout();
    }

    public void updateTitle(String title) {
        mTitle.setText(title);
        mBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) context).finish();
            }
        });
    }

    @Override
    public void notifyAnim(int i) {

    }

    @Override
    public void registToManager(IPositionManager iPositionManager) {

    }

    @Override
    public void unreistFromManager(IPositionManager iPositionManager) {

    }

    @Override
    public int[] getLocations() {
        return new int[0];
    }
}
